package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.diyview.CenteredEditText;
import com.topboya.linker.diyview.bottomview.NavigationController;
import com.topboya.linker.diyview.bottomview.PageNavigationView;
import com.topboya.linker.diyview.bottomview.custom.SpecialTab;
import com.topboya.linker.diyview.bottomview.custom.SpecialTabRound;
import com.topboya.linker.diyview.bottomview.listener.OnTabItemSelectedListener;
import com.topboya.linker.model.levelitem.DepartmentGroupBo;
import com.topboya.linker.model.levelitem.FollowUserGroup;
import com.topboya.linker.model.response.DeptUserResponse;
import com.topboya.linker.ui.adapter.SelecteamAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.fragment.HomeFragment;
import com.topboya.linker.ui.fragment.MeFragment;
import com.topboya.linker.ui.fragment.MessageFragment;
import com.topboya.linker.ui.presenter.MainPrensenter;
import com.topboya.linker.ui.view.MainView;
import com.topboya.linker.utils.LogUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class xMainActivity extends BaseActivity<MainView, MainPrensenter> implements MainView {

    private static final String TAG = "MainActivity-";
    private TextView tv_tool_title;
    private LinearLayout ll_task_btn;
    private Button btn_my_tasklist;
    private Button btn_team_tasklist;
    private CenteredEditText search;
    private ImageView img_search;
    private Button btn_cancle_search;
    private LinearLayout ll_search;
    private List<DeptUserResponse.Data> mData;
    private HomeFragment homeFragment;
    private MeFragment meFragment;
    private MessageFragment messageFragment;
    private  PageNavigationView tab;
    private NavigationController controller;
    private Toolbar toolbar;
    @Override
    protected int getLayoutId() {

        return R.layout.xactivity_main;
    }

    @Override
    protected void initView() {

          tab = findViewById(R.id.tab);
          ll_task_btn = findViewById(R.id.ll_task_btn);
          btn_my_tasklist = findViewById(R.id.btn_my_tasklist);
          btn_team_tasklist = findViewById(R.id.btn_team_tasklist);
          search = findViewById(R.id.search);
          img_search = findViewById(R.id.img_search);
          btn_cancle_search = findViewById(R.id.btn_cancle_search);
          tv_tool_title = findViewById(R.id.tv_tool_title);
          ll_search = findViewById(R.id.ll_search);
          toolbar = findViewById(R.id.toolbar);
          initToolbar();
          initBottomView();
          homeFragment = new HomeFragment();
          meFragment = new MeFragment();
          changeToFragment(homeFragment, R.id.container);
    }

    private void initToolbar() {


    }

    @Override
    protected void initListener() {

        img_search.setOnClickListener(v -> {

            tv_tool_title.setVisibility(View.GONE);
            ll_task_btn.setVisibility(View.GONE);
            img_search.setVisibility(View.GONE);
            ll_search.setVisibility(View.VISIBLE);


        });


        btn_cancle_search.setOnClickListener(v -> {

            ll_search.setVisibility(View.GONE);
            if (null==mData ||mData.isEmpty()){
//                表示没有下属
                tv_tool_title.setVisibility(View.VISIBLE);
            }else {
                ll_task_btn.setVisibility(View.VISIBLE);
            }
            img_search.setVisibility(View.VISIBLE);

        });

        btn_my_tasklist.setOnClickListener(v -> {

            AppConst.isMyTeam =false;
           btn_my_tasklist.setBackgroundResource(R.drawable.home_page_my_selected_bg);
           btn_team_tasklist.setBackgroundResource(R.drawable.home_page_team_noselected_bg);
            teamBtnClickListener.onClick(true);


        });
        btn_team_tasklist.setOnClickListener(v -> {

            AppConst.isMyTeam =true;
            btn_team_tasklist.setBackgroundResource(R.drawable.home_page_team_selected_bg);
            btn_my_tasklist.setBackgroundResource(R.drawable.home_page_my_noselected_bg);
            teamBtnClickListener.onClick(false);
        });
    }

   public void  initBottomView(){

        controller = tab.custom()
               .addItem(newItem(R.drawable.home_default,R.drawable.me_checked,"首页"))
               .addItem(newRoundItem(R.drawable.createtask,R.drawable.createtask,"创建任务"))
               .addItem(newItem(R.drawable.me_default,R.drawable.me_checked,"我的"))
               .build();

       controller.addTabItemSelectedListener(new OnTabItemSelectedListener() {
           @Override
           public void onSelected(int index, int old) {

//               Toast.makeText(xMainActivity.this,"index:"+index,Toast.LENGTH_SHORT).show();
               switch (index){
                   case 0:
                       toolbar.setVisibility(View.VISIBLE);
                       btn_my_tasklist.setBackgroundResource(R.drawable.home_page_my_selected_bg);
                       btn_team_tasklist.setBackgroundResource(R.drawable.home_page_team_noselected_bg);
                       homeFragment = new HomeFragment();
                       changeToFragment(homeFragment,R.id.container);
                       break;
                   case 1:
//                      先将上次创建任务选择的资料清空
                       AppConst.FILE_SELECTED_MAP.clear();
                       Intent intent = new Intent(xMainActivity.this,xCreateTaskActivity.class);
                        startActivityForResult(intent,1001);
//                       jumpToActivity(xCreateTaskActivity.class);
                       break;
                   case 2:
                       toolbar.setVisibility(View.GONE);
                       messageFragment = new MessageFragment();
                       changeToFragment(messageFragment,R.id.container);
               }
           }

           @Override
           public void onRepeat(int index) {

           }
       });
   }
    /**
     * 正常tab
     */
    private SpecialTab newItem(int drawable, int checkedDrawable, String text){

        SpecialTab mainTab = new SpecialTab(this);
        mainTab.initialize(drawable,checkedDrawable,text);
        mainTab.setTextDefaultColor(getResources().getColor(R.color.home_bottom_text_default));
        mainTab.setTextCheckedColor(getResources().getColor(R.color.home_bottom_text_checked));
        return mainTab;
    }

    /**
     * 圆形tab
     */
    private SpecialTabRound newRoundItem(int drawable, int checkedDrawable, String text){

        SpecialTabRound mainTab = new SpecialTabRound(this);
        mainTab.initialize(drawable,checkedDrawable,text);
        mainTab.setTextDefaultColor(getResources().getColor(R.color.home_bottom_text_default));
        mainTab.setTextCheckedColor(getResources().getColor(R.color.home_bottom_text_checked));
        return mainTab;
    }


    public void changeToFragment(Fragment fragment, int layout) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            fragmentTransaction.replace(layout, fragment);
            fragmentTransaction.commitAllowingStateLoss();
        } else {
            fragmentTransaction.show(fragment);
        }
    }
    @Override
    protected MainPrensenter createPresenter() {

        return new MainPrensenter(this);
    }

    @Override
    protected void initData() {
          LogUtils.v(TAG + "initData()");
           mPresenter.getDeptUserList();
//        DeptUserResponse deptUserResponse = new DeptUserResponse();
//        List<DeptUserResponse.Data> datalist = new ArrayList<>();
//        DeptUserResponse.Data data = deptUserResponse.new Data();
//        data.setName("博雅公司");
//
//        List<DeptUserResponse.SubDeptList> subDeptLists = new ArrayList<>();
//
//        DeptUserResponse.SubDeptList subDept1 = deptUserResponse.new SubDeptList();
//        subDept1.setName("信息部门");
//        DeptUserResponse.SubDeptList subDept2 = deptUserResponse.new SubDeptList();
//        subDept2.setName("财务部门");
//        DeptUserResponse.SubDeptList subDept3 = deptUserResponse.new SubDeptList();
//        subDept3.setName("行政部门");
//
//        subDeptLists.add(subDept1);
//        subDeptLists.add(subDept2);
//        subDeptLists.add(subDept3);
//
//
//        List<DeptUserResponse.SubDeptList> subDeptLists1 = new ArrayList<>();
//        DeptUserResponse.SubDeptList subDeptListq = deptUserResponse.new SubDeptList();
//        subDeptListq.setName("信息部门1");
//        DeptUserResponse.SubDeptList subDeptListw = deptUserResponse.new SubDeptList();
//        subDeptListw.setName("财务部门2");
//        DeptUserResponse.SubDeptList subDeptListe = deptUserResponse.new SubDeptList();
//        subDeptListe.setName("行政部门3");
//
//        subDeptLists1.add(subDeptListq);
//        subDeptLists1.add(subDeptListw);
//        subDeptLists1.add(subDeptListe);
//        subDept1.setSubDeptList(subDeptLists1);
//
//
//        List<DeptUserResponse.SubDeptList> subDeptLists2 = new ArrayList<>();
//
//        DeptUserResponse.SubDeptList subDeptListr = deptUserResponse.new SubDeptList();
//        subDeptListr.setName("信息部门1");
//        DeptUserResponse.SubDeptList subDeptListt = deptUserResponse.new SubDeptList();
//        subDeptListt.setName("财务部门1");
//        DeptUserResponse.SubDeptList subDeptListy = deptUserResponse.new SubDeptList();
//        subDeptListy.setName("行政部门1");
//
//        subDeptLists2.add(subDeptListr);
//        subDeptLists2.add(subDeptListt);
//        subDeptLists2.add(subDeptListy);
//        subDept1.setSubDeptList(subDeptLists2);
//
//
//        List<DeptUserResponse.DeptUserList> deptUserLists = new ArrayList<>();
//
//        DeptUserResponse.DeptUserList deptUserList = deptUserResponse.new DeptUserList();
//        deptUserList.setName("张珊");
//        DeptUserResponse.DeptUserList deptUserList1 = deptUserResponse.new DeptUserList();
//        deptUserList1.setName("李四");
//        DeptUserResponse.DeptUserList deptUserList2 = deptUserResponse.new DeptUserList();
//        deptUserList2.setName("网二");
//        DeptUserResponse.DeptUserList deptUserList3 = deptUserResponse.new DeptUserList();
//        deptUserList3.setName("王柳");
//
//        deptUserLists.add(deptUserList);
//        deptUserLists.add(deptUserList1);
//        deptUserLists.add(deptUserList2);
//        deptUserLists.add(deptUserList3);
//
//        subDeptListr.setDeptUserList(deptUserLists);
//        data.setSubDeptList(subDeptLists);
//        datalist.add(data);

//       getDeptUserList(datalist);

    }


    @Override
    public void getDeptUserList(List<DeptUserResponse.Data> mData) {

        this.mData = mData;
        if (null == mData) {
            // 普通员工,显示首页，部门和个人切换按钮关闭
            ll_task_btn.setVisibility(View.GONE);
            tv_tool_title.setVisibility(View.VISIBLE);

        } else {
            //部门领导,部门切换按钮显示
            tv_tool_title.setVisibility(View.GONE);
            ll_task_btn.setVisibility(View.VISIBLE);

        }
    }

    public List<DeptUserResponse.Data> getmData(){

        return  mData;
    }

    public TeamBtnClickListener teamBtnClickListener;

    public void setOnTeamBtnClickListener(TeamBtnClickListener teamBtnClickListener){

        this.teamBtnClickListener = teamBtnClickListener;

    }
    public  interface TeamBtnClickListener{

      public  void onClick(boolean ismytask);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        华为手机的onActivityResult不执行，网上说是由于activityB额的启动模式不是startand
        LogUtils.v(TAG + "requestCode："+requestCode+"resultCode:"+resultCode);
        if (requestCode ==1002){

            btn_my_tasklist.setBackgroundResource(R.drawable.home_page_my_selected_bg);
            btn_team_tasklist.setBackgroundResource(R.drawable.home_page_team_noselected_bg);
        }
          controller.setSelect(0);


      }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

         LogUtils.v(TAG +"onNewIntent()");
//        btn_my_tasklist.setBackgroundResource(R.drawable.home_page_my_selected_bg);
//        btn_team_tasklist.setBackgroundResource(R.drawable.home_page_team_noselected_bg);
//        controller.setSelect(0);
//        refreshMemberListener.refreshMember();
        boolean isfeedback = intent.getBooleanExtra("isfeedback",true);
         LogUtils.v(TAG+"isfeedback:--->"+isfeedback);
//           反馈页面返回
        if (isfeedback){

            controller.setSelect(0);
            homeFragment = new HomeFragment();
            changeToFragment(homeFragment,R.id.container);
        }else {

            refreshMemberListener.refreshMember();
        }


    }

    public void SetOnrefreshMemberListener(RefreshMemberListener refreshMemberListener){

        this.refreshMemberListener =refreshMemberListener;
    }

     private  RefreshMemberListener refreshMemberListener;
//    刷新选择部门成员的监听，从搜索页面跳转过来的
     public  interface RefreshMemberListener{

        public void refreshMember();
    }




}
